package org.myblog.myblogserver.code.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.myblog.myblogserver.code.entity.User;
import org.myblog.myblogserver.code.service.UserService;
import org.myblog.myblogserver.code.service.impl.UserServiceImpl;
import org.myblog.myblogserver.config.GlobalConfig;
import org.myblog.myblogserver.config.RedisConfig;
import org.myblog.myblogserver.dao.Result;
import org.myblog.myblogserver.dao.UserDao;
import org.myblog.myblogserver.dao.UserLoginDao;
import org.myblog.myblogserver.dao.UserPage;
import org.myblog.myblogserver.util.SendCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import java.util.List;
import java.util.UUID;
import java.util.concurrent.TimeUnit;



@RestController
@RequestMapping("/user")
@Api(tags = "User")
public class UserController {
    private Result result=new Result();

    @Autowired
    private UserService userService;

    //增加
    @Autowired
    private StringRedisTemplate stringRedisTemplateTemplate;

    /**
     * post  /user/login<br/>
     * json<br/>
     * userdao 里有两个参数user和code<br/>
     * user里面的email必须有<br/>
     * user里的密码和code必须有<br/>
     *
     * @author 智哥
     * @param userdao
     * @return Result
     * */
    @PostMapping("/login")
    @ApiOperation(value = "User登录")
    public Result login(@RequestBody UserDao userdao) {
        UserLoginDao userLoginDao = userService.login(userdao.getUser(), userdao.getCode());
        if(userLoginDao != null){
            result.success("登录成功", userLoginDao);
        }
        else{
            result.client_fail("没有当前用户信息", null);
        }
        return result;
    }

    /**
     * post  /user/sign<br/>
     * json<br/>
     * userdao 里有两个参数user和code<br/>
     * 除了id和status为null<br/>
     * 其他全都要必须有<br/>
     *
     * @author 智哥
     * @param userdao
     * @return Result
     * */
    @PostMapping("/sign")
    @ApiOperation(value = "User注册")
    public Result sign(@RequestBody UserDao userdao) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", userdao.getUser().getEmail());
        User user =userService.getOne(queryWrapper);
        if(user != null){
            result.client_fail("邮箱已存在", null);
            System.out.println(result);
            return result;
        }
        if(userService.sign(userdao.getUser(), userdao.getCode())){
            result.success("注册成功", null);
        }
        else{
            result.client_fail("验证码错误", null);
        }
        return result;
    }


    /**
     * post  /user/sendcode<br/>
     * 在url里面接 ?email=2445219557@.com<br/>
     *
     * @author 智哥
     * @param email
     * @throws Exception 邮件发送失败
     * @return Result
     * */
    @PostMapping("/sendcode")
    @ApiOperation(value = "发送验证码")
    public Result SendCode(@RequestParam("email") String email) throws Exception {
        SendCode sendCode = new SendCode();
        String code=sendCode.getCode(6);
        stringRedisTemplateTemplate.opsForValue().set(GlobalConfig.CODETEXT+email, code, GlobalConfig.OUTTIME, TimeUnit.MINUTES);
        sendCode.sendEmail(email, code, GlobalConfig.EmailContent);
        result.success("发送成功", null);
        return result;
    }



    /**
     * post  /user/selecteq<br/>
     * 不给条件全查，都是eq查询<br/>
     *
     * @author 智哥
     * @param user
     * @return Result
     * */
    @ApiOperation(value = "User条件查询")
    @PostMapping("/selecteq")
    public Result selecteq(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(user.getId() != null) queryWrapper.like("id", user.getId());
        if(user.getUsername() != null) queryWrapper.like("username", user.getUsername());
        if(user.getPassword() != null) queryWrapper.like("password", user.getPassword());
        if(user.getEmail() != null) queryWrapper.like("email", user.getEmail());
        if(user.getState() != null) queryWrapper.like("state", user.getState());
        result.success("查询成功", userService.list(queryWrapper));
        return result;
    }


    /**
     * post  /user/update<br/>
     * 必须给email,email、id不能改<br/>
     *
     * @author 智哥
     * @param user
     * @return Result
     * */
    @ApiOperation(value = "User修改")
    @PostMapping("/update")
    public Result update(@RequestBody User user) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("email", user.getEmail());
        if(user.getUsername() != null) updateWrapper.set("username", user.getUsername());
        if(user.getPassword() != null) updateWrapper.set("password", user.getPassword());
        if(user.getState() != null) updateWrapper.set("state", user.getState());
        Boolean b =userService.update(updateWrapper);
        if(b){
            result.success("修改成功", null);
        }
        else{
            result.server_fail("修改失败", null);
        }
        return result;
    }

    /**
     * post  /user/deleteAll<br/>
     * 什么都不给全删<br/>
     * eq删除<br/>
     *
     * @author 智哥
     * @param user
     * @return Result
     * */
    @ApiOperation(value = "User条件删除")
    @PostMapping("/deleteAlleq")
    public Result deleteAlleq(@RequestBody User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(user.getId() != null) queryWrapper.eq("id", user.getId());
        if(user.getUsername() != null) queryWrapper.eq("username", user.getUsername());
        if(user.getPassword() != null) queryWrapper.eq("password", user.getPassword());
        if(user.getEmail() != null) queryWrapper.eq("email", user.getEmail());
        if(user.getState() != null) queryWrapper.eq("state", user.getState());
        if(userService.remove(queryWrapper)){
            result.success("删除成功", true);
        }
        else{
            result.server_fail("删除失败", false);
        }
        return result;
    }

    /**
     * post  /user/selectpage<br/>
     * userpage里面有index和page<br/>
     * index是页码，从1开始<br/>
     * page是每页数量<br/>
     * user用相似查询<br/>
     *
     * @author 智哥
     * @param userpage
     * @return Result
     * */
    @PostMapping("/selectpage")
    @ApiOperation(value = "User分页查询")
    public Result selectPage(@RequestBody UserPage userpage) {
        User user = userpage.getUser();
        IPage<User> page = new Page<>(userpage.getIndex(), userpage.getPage());
        System.out.println("page = " + page);
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        if(user.getUsername()!=null) queryWrapper.like("username", user.getUsername());
        if(user.getEmail()!=null) queryWrapper.like("email", user.getEmail());
        IPage<User> userIPage = userService.page(page, queryWrapper);
        List<User> list = userIPage.getRecords();
        if (list.isEmpty() && userpage.getIndex() > 1) {
            result.client_fail("当前页码没有数据",null);
        } else {
            result.success("查询成功", list);
        }
        result.success("查询成功", list);
        return result;
    }


    /**
     * get  /user/selectCount<br/>
     * User记录数查询<br/>
     *
     * @author 智哥
     * @return Result
     * */
    @GetMapping("/selectCount")
    @ApiOperation(value = "User记录数查询")
    public Result selectCount() {
        long a=userService.count();
        if(a>=0){
            result.success("查询成功",a);
        }
        else {
            result.server_fail("查询失败",0);
        }
        return result;
    }
}